#!/usr/bin/python3
#去除 wine 微信的黑色边框
#必须首先安装 xdotool
import os
import sys
import time
import re

def get_width( wid ):
	for line1 in os.popen('xdotool getwindowgeometry %s' % wid).readlines():
		ret=re.findall('^\\s*Geometry:\\s*([0-9]+)x[0-9]+',line1)
		if len(ret)>0:
			print('width: '+ret[0])
			return int(ret[0])
	return -1
def remove_shadow(app_class):
	for wid in os.popen('xdotool search --onlyvisible --class ' + app_class).readlines():
		if len(wid) > 1:
			wdn = os.popen('xdotool getwindowname %s' % wid[:-1]).readline()[:-1]
			if wdn == '': # 移除掉装饰窗口
				width = get_width(wid[:-1])
				if width > 200: # 防止关闭点击图标时的弹出菜单
					os.system('xdotool windowunmap %s' % wid[:-1])
					print('unmap: window id: %s; window name: %s' % (wid[:-1], wdn))
def check_app_running(app_exe):
	result = os.popen("ps -ef | grep %s | awk '{print \"/proc/\"$2\"/cwd\"}' | xargs ls -l | grep drive_c" % app_exe).readlines()
	return len(result) > 0
def handle(app_exe, app_class):
	check_app_alive_max_count = 0
	while True:
		running = check_app_running(app_exe)
		if running:
			remove_shadow(app_class)
			check_app_alive_max_count = 0
		else:
			check_app_alive_max_count = check_app_alive_max_count + 1
		if check_app_alive_max_count > 3: # 三次没有检测程序启动，退出
			exit()
		time.sleep(5) # 休眠5秒
if __name__ == '__main__':
	exe_name = 'WeChat.exe'
	class_name = 'WeChat.exe'
	handle(exe_name,class_name)


